﻿using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MSAccess_Interactive
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Info> infos = DB.Access.Select<Info>().ToList();
            foreach(Info info in infos)
            {
                Console.WriteLine($"Id={info.Id},Name={info.Name},Age={info.Age}");
            }

            Info info_new = new Info()
            {
                Name="bl",
                Age=11,
                Score=95.6f
            };
            int rows=DB.Access.Insert<Info>(info_new).ExecuteAffrows();
            Console.WriteLine($"影响的行数:{rows}");

            Console.ReadKey();
        }
    }

    class DB
    {
        static string connect_string = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=student.mdb";
        static Lazy<IFreeSql> accessLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
           .UseMonitorCommand(cmd => Trace.WriteLine($"Sql：{cmd.CommandText}"))//监听SQL语句,Trace在输出选项卡中查看
           .UseConnectionString(FreeSql.DataType.MsAccess, connect_string)
           .UseAutoSyncStructure(true) //自动同步实体结构到数据库，FreeSql不会扫描程序集，只有CRUD时才会生成表。
           .Build());

        public static IFreeSql Access
        {
            get
            {
                return accessLazy.Value;
            }
        }
    }

    [Table(Name = "tbl_info")]
    class Info
    {
        [Column(Name = "_id")]
        public int Id { set; get; }

        [Column(Name = "_name")]
        public string Name { set; get; }

        [Column(Name = "_age")]
        public int Age { set; get; }

        [Column(Name="_score")]
        public float Score { set; get; }
    }

}
